package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国民用航空网  网站解析
 * @author Chase
 *
 */
public class CcaonlineNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.ccaonline.cn/.*/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/[0-9]*_[0-9]*.shtml",
		"http://.*.ccaonline.cn/Item/[0-9]*.html",
		"http://.*.ccaonline.cn/.*/.*/[0-9]*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title=doc.select("title").text();
		title=StringUtils.substringBefore(title, "_");

		date=DateUtils.matchDate(doc.select(".tie-date").text());

		content=doc.select(".entry").html();
		
		news.setUrl(url);

		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		CcaonlineNewsAnalyse analyse=new CcaonlineNewsAnalyse();
		String url="http://news.ccaonline.cn/Item/154342.html";
		System.out.println(analyse.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
//		System.err.println(meta.getHtml());
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
